package net.huazai.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.huazai.entity.AddressAddEntity;
import net.huazai.entity.AddressEditEntity;
import net.huazai.enums.BizCodes;
import net.huazai.service.UserAddressService;
import net.huazai.utils.ApiResult;
import net.huazai.vo.UserAddressVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 11987
 */
@Api(tags = "收货地址模块")
@RestController
@RequestMapping("/api/address/v1/")
public class UserAddressController {

    @Autowired
    private UserAddressService userAddressService;

    /**
     * 新增收货地址
     * @param addressAddEntity 收货地址对象
     * @return 添加结果
     */
    @ApiOperation("新增收货地址")
    @PostMapping("add")
    public ApiResult add(
            @ApiParam("收货地址对象") @RequestBody AddressAddEntity addressAddEntity) {
        userAddressService.add(addressAddEntity);
        return ApiResult.doSuccess();
    }

    /**
     * 根据id查找收货地址
     * @param addressId 收货地址id
     * @return 查询结果
     */
    @ApiOperation("根据id查找收货地址")
    @GetMapping("detail/{address_id}")
    public Object findDetail(@ApiParam(value = "收货地址id", required = true)
            @PathVariable("address_id") Long addressId) {
        UserAddressVO userAddressVo = userAddressService.detail(addressId);
        return userAddressVo == null ? ApiResult.doResult(
                BizCodes.USER_ADDRESS_NOT_EXITS)
                : ApiResult.doSuccess(userAddressVo);
    }

    /**
     * 修改指定收货地址
     * @param addressEditEntity 修改收货地址对象
     * @return 修改结果
     */
    @ApiOperation("修改指定收货地址")
    @PostMapping("edit")
    public ApiResult edit(
            @ApiParam("收货地址对象") @RequestBody AddressEditEntity addressEditEntity) {
        userAddressService.edit(addressEditEntity);
        return ApiResult.doSuccess();
    }

    /**
     * 删除指定收货地址
     * @param addressId 收货地址id
     * @return 删除结果
     */
    @ApiOperation("删除指定收货地址")
    @DeleteMapping("del/{address_id}")
    public ApiResult del(@ApiParam(value = "收货地址id", required = true)
                         @PathVariable("address_id") Long addressId) {
        int del = userAddressService.del(addressId);
        return del == 1 ? ApiResult.doSuccess() : ApiResult.doResult(
                BizCodes.USER_ADDRESS_DEL_FAIL);
    }

    /**
     * 查询用户全部收货地址
     * @return 查询结果
     */
    @ApiOperation("查询用户全部收货地址")
    @GetMapping("list")
    public ApiResult findAllAddress() {
        List<UserAddressVO> list = userAddressService.findAllAddress();
        return ApiResult.doSuccess(list);
    }
}

